package th.ac.tu.engr.timemylife;

import android.app.Activity;
import android.app.Notification;
import android.app.NotificationManager;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.util.Log;
import android.content.Intent;
import org.json.JSONObject;
import java.net.MalformedURLException;
import java.net.URL;


public class MainActivity extends Activity {

    private static final String TAG = "MainActivity";
    private static boolean connectFeature = false;
    private static int locationFeature = 0;
    private static int appFeature = 0;
    private static int IDs = 1000;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        // TODO: check shared preference, create if not exist
        SharedPreferences pref = MyApp.getAppPreference();
        if (pref.contains(getString(R.string.pref_connect))) {
            connectFeature = pref.getBoolean(getString(R.string.pref_connect), false);
        } else {
            SharedPreferences.Editor pref_editor = pref.edit();
            pref_editor.putBoolean(getString(R.string.pref_connect), connectFeature);
            pref_editor.putInt("ID", 0);
            pref_editor.apply();
        }
        // TODO: other preference options
        // Start background service
        Intent serviceIntent = new Intent(this, MyService.class);
        startService(serviceIntent);
        // TODO: Start persistence notification
        Notification.Builder ntfBuilder =  new Notification.Builder(this);
        ntfBuilder.setContentTitle("Time My Life");
        ntfBuilder.setContentText("by Thammasat University");
        ntfBuilder.setSmallIcon(R.drawable.ic_launcher);
        NotificationManager ntfManager = (NotificationManager)getSystemService(NOTIFICATION_SERVICE);
        ntfManager.notify(1, ntfBuilder.build());
        // Start UI
        setContentView(R.layout.activity_main);
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        SharedPreferences pref = MyApp.getAppPreference();
        SharedPreferences.Editor pref_editor = pref.edit();
        int ID = pref.getInt("ID", 0);
        MenuItem item;
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.menu_main, menu);
        // TODO: change text of menu item according to preferences
        item = menu.findItem(R.id.action_connect);
        if (connectFeature) {
            item.setTitle(getString(R.string.action_connect) + "enable");
        } else {
            item.setTitle(getString(R.string.action_connect) + "disable");
        }
        // TODO: change text of menu item according to ID
        item = menu.findItem(R.id.action_view);
        if (ID > 0) {
            item.setTitle(getString(R.string.action_view) + ID);
        } else {
            item.setTitle(getString(R.string.action_view) + "not active");
        }

        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        SharedPreferences pref = MyApp.getAppPreference();
        SharedPreferences.Editor pref_editor = pref.edit();
        int ID = pref.getInt("ID",0);
        int id = item.getItemId();
        // Handle menu items
        if (id == R.id.action_connect) {
            // TODO: set preference for server connection
            // TODO: update menu item
            if (connectFeature) {
                connectFeature = false;
                item.setTitle(getString(R.string.action_connect) + "disable");
                Log.v(TAG, "Already have ID");

            } else {
                // TODO: if no ID, connect with server for ID
                item.setTitle(getString(R.string.action_connect) + "enable");
                if (ID == 0) {
                    ServerAsyncTask registerTask = new ServerAsyncTask();
                    try {
                        Log.v(TAG, "Register");

                        URL url = new URL("http://time-my-life.appspot.com/register?id=" + IDs++);
                        registerTask.execute(url);

                    } catch (MalformedURLException e) {
                        e.printStackTrace();
                    }
            }
            }
            pref_editor.putBoolean(getString(R.string.pref_connect), connectFeature);
            pref_editor.apply();
            return true;
        }
        if (id == R.id.action_location) {
            Log.v(TAG, "Menu Location");
            // TODO: set preference for location recording and change menu status

            // TODO: update menu item
            return true;
        }

        if (id == R.id.action_view) {
            Log.v(TAG, "View ID");
            // TODO: check if ID is active
            if (ID > 0) {
                item.setTitle(getString(R.string.action_view) + ID);
                Intent intent = new Intent("com.google.zxing.client.android.ENCODE");
                intent.putExtra("ENCODE_TYPE", "TEXT_TYPE" );
                intent.putExtra("ENCODE_DATA", "http://time-my-life.appspot.com/view?id="+ID);
                intent.putExtra("ENCODE_FORMAT", "QR_CODE");
                intent.putExtra("ENCODE_SHOW_CONTENTS", true );
                startActivity(intent);
            } else {
                item.setTitle(getString(R.string.action_view) + "not active");
            }
            // TODO: update menu item
            return true;
        }

        if (id == R.id.action_settings) {
            Log.v(TAG, "Menu Settings");
            // TODO: open setting screen
            // TODO: update menu item
            return true;
        }

        return super.onOptionsItemSelected(item);
    }
}
